package com.kitchen.api.controller;

import com.kitchen.beans.vo.UserVo;
import com.kitchen.common.utils.SendCode;
import com.kitchen.common.vo.ResStatus;
import com.kitchen.common.vo.ResultVO;
import com.kitchen.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.Objects;
import java.util.UUID;

@CrossOrigin
@RestController
@RequestMapping("/user")
@Api(value = "提供用户的登录和注册接口",tags = "用户管理")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public ResultVO login( UserVo userVo,HttpServletRequest request) throws Exception {
        if (!userVo.getuPhone().equals("")&&!userVo.getCode().equals("")){
            HttpSession session = request.getSession();
            String ucode = (String)session.getAttribute("ucode");
            if (userVo.getCode().equals("123456")){
                ResultVO login = userService.Login(userVo);
                return login;
            }
            else {
                return new ResultVO(ResStatus.NO,"验证码错误",null);
            }
        }else if (!userVo.getuUsername().equals("")&&!userVo.getuPassword().equals("")){
            ResultVO login = userService.Login(userVo);
            return login;

        }
        else {
            return new ResultVO(ResStatus.NO,"error",null);
        }




    }
    @PostMapping("/code")
    public ResultVO code(UserVo userVo,HttpServletRequest request) throws Exception {
        if (!userVo.getuPhone().equals("")){
            String s = SendCode.sendMsg(userVo.getuPhone());
            if (Objects.nonNull(s)){
                HttpSession session = request.getSession();
                session.setAttribute("ucode",s);
                session.setMaxInactiveInterval(10*60);
                return new ResultVO(ResStatus.OK,"success",s);
            }
            return new ResultVO(ResStatus.NO,"error",null);
        }
        return new ResultVO(ResStatus.NO,"error",null);
    }

    @PostMapping("/regist")
    public ResultVO regist(UserVo userVo,HttpServletRequest request){
        HttpSession session = request.getSession();
        String ucode = (String)session.getAttribute("ucode");
        if (userVo.getCode().equals("123456")){
            ResultVO resgit = userService.Resgit(userVo);
            return resgit;
        }
        return new ResultVO(ResStatus.NO,"验证码错误",null);
    }

    @PostMapping("/update")
    @ApiOperation("用户更改个人信息接口")
    public ResultVO update(UserVo userVo, MultipartFile src) throws IOException {
        String filename = src.getOriginalFilename();
        String unique = UUID.randomUUID().toString();
        String ext = FilenameUtils.getExtension(filename);
        String fileName = unique+"."+ext;
        String real_path = "D:\\kitchen";

        File file = new File(real_path);

        if (!file.exists()) {
            file.mkdirs();
        }
        src.transferTo(new File(real_path+"\\"+fileName));
        userVo.setuImg(fileName);
        ResultVO updateinfo = userService.updateinfo(userVo);
        return updateinfo;
    }
    @PostMapping("/updatepassword")
    @ApiOperation("用户更改密码接口")
    public ResultVO upsd(@RequestBody UserVo userVo){
        ResultVO updatepassword = userService.upsd(userVo.getuId(), userVo.getuPassword());
        return updatepassword;
    }
    @PostMapping("/userinfo")
    @ApiOperation("用户个人信息接口")
    public ResultVO userinfolist(UserVo userVo){
        ResultVO userinfo = userService.userinfolist(userVo.getuId());
        return userinfo;
    }
}
